package com.example.administrator.yuejian2.fragment;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.activity.LoginActivity;
import com.example.administrator.yuejian2.activity.MyAttentionActivity;
import com.example.administrator.yuejian2.activity.MyOrderActivity;
import com.example.administrator.yuejian2.activity.MySetActivity;
import com.example.administrator.yuejian2.activity.ShoppingCartActivity;
import com.example.administrator.yuejian2.activity.VLayoutActivity;
import com.example.administrator.yuejian2.activity.duobujuActivity;
import com.example.administrator.yuejian2.bean.MeBean;
import com.example.administrator.yuejian2.model.MyModel;
import com.example.administrator.yuejian2.other.ActivityManage;
import com.squareup.picasso.Picasso;

import java.io.File;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment {

    private SharedPreferences sp;
    private TextView spTextview;
    private String user_id;
    private Button clear,button,button1;
    private ImageView touxiang;
    private RetrofitListener<MeBean> listener;
    private LinearLayout layout1, layout2, layout3, layout4, layout6, layout5;

    public MeFragment() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        spTextview = view.findViewById(R.id.spTextView);
        touxiang = view.findViewById(R.id.me_img_log);
        initView(view);
        initEvents();
        sp = getActivity().getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getString("userid", null);
        if (user_id == null) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("您当前还未登陆");
            builder.setMessage("是否登录？");
            builder.setCancelable(false);
            builder.setPositiveButton("去登陆", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);

                }
            });
            builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.show();
        } else {
            myInfo();
        }


    }

    public static void cleanSharedPreference(Context context) {
        deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs"));
    }

    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.layout1_me:
                    Intent intent = new Intent(getActivity(), MySetActivity.class);
                    startActivity(intent);
                    break;
                case R.id.layout2_me:
                    Intent intent1 = new Intent(getActivity(), MyOrderActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.layout3_me:
                    break;
                case R.id.layout4_me:
                    break;
                case R.id.layout5_me:
                    Intent intent5 = new Intent(getActivity(), MyAttentionActivity.class);
                    startActivity(intent5);
                    break;
                case R.id.layout6_me:
                    break;
                default:
                    break;
            }
        }
    };

    private void myInfo() {
        MyModel myModel = new MyModel();
        myModel.myInfo(user_id, listener);
    }


    private void initView(View view) {
        clear = view.findViewById(R.id.clear);
        button1=view.findViewById(R.id.duobujubutton);
        button=view.findViewById(R.id.CoordinatorLayout_button);
        layout1 = view.findViewById(R.id.layout1_me);
        layout2 = view.findViewById(R.id.layout2_me);
        layout3 = view.findViewById(R.id.layout3_me);
        layout4 = view.findViewById(R.id.layout4_me);
        layout6 = view.findViewById(R.id.layout6_me);
        layout5 = view.findViewById(R.id.layout5_me);

    }

    private void initEvents() {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout6.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanSharedPreference(getActivity());
                Toast.makeText(getActivity(), "退出登录成功", Toast.LENGTH_SHORT).show();
                ActivityManage.finishAll();
                android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
                System.exit(0);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),duobujuActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        listener = new RetrofitListener<MeBean>() {
            @Override
            public void onSuccess(MeBean meBean, int flag) {
                spTextview.setText(meBean.getUser_name());
                Picasso.with(getContext()).load(IMAGEURL + meBean.getUser_pic())
                        .placeholder(R.mipmap.loading)
                        .into(touxiang);
            }

            @Override
            public void onFail() {

            }
        };
        return inflater.inflate(R.layout.fragment_me, container, false);
    }

}
